package com.example.qjia.util.retrofit;

import android.content.Context;
import android.net.ConnectivityManager;
import android.net.Network;
import android.net.NetworkInfo;
import android.util.Log;

import com.example.qjia.basic.MyApplication;
import com.example.qjia.util.cookies.CookiesManager;

import java.io.File;
import java.io.IOException;

import okhttp3.Cache;
import okhttp3.CacheControl;
import okhttp3.Interceptor;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava.RxJavaCallAdapterFactory;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 * Created by ying_xiang on 2016/8/17.
 */
public class OkHttpUtils implements OkHttpUtilsInterface {
    private QjiaService qjiaService;
     //private Context mContext=MyApplication.getInstance().getApplicationContext();
    private static final String TAG="OkHttpUtils";

    private Context mcontext=MyApplication.getInstance().getApplicationContext();
    @Override
    public QjiaService getService() {

        File cacheFile=new File(mcontext.getExternalCacheDir(),"QjiaCache");
        Cache cache=new Cache(cacheFile,1024*1024*5);    //设置为五兆;
        Interceptor interceptor=new Interceptor() {
            @Override
            public Response intercept(Chain chain) throws IOException {
                Request request=chain.request();
                if(!isNetOk(mcontext)){
                   // Log.i(TAG, "intercept: net is no");
                    System.out.println("net is no");
                    request=request.newBuilder()
                            .cacheControl(CacheControl.FORCE_CACHE)    //没有网的时候只从缓存中获取
                            .build();

                }
                Response response=chain.proceed(request);  //执行请求后返回
                if(isNetOk(mcontext)){
                   System.out.println("net is ok");
                    int maxAge=1*60; //一分钟
                    response=response.newBuilder()

                            .header("Cache-Control", "public, max-age="+ maxAge)
                            .removeHeader("Pragma")
                            .build();
                }else {
                    int maxAge=4*7*60*24*60;//四周
                    response=response.newBuilder()
                            .header("Cache-Control", "public, only-if-cached, max-stale=" + maxAge)
                            .removeHeader("Pragma")
                            .build();
                }
                return response;
            }
        };
        OkHttpClient okHttpClient=new OkHttpClient.Builder().
                cache(cache)
                .cookieJar(new CookiesManager()) // 实现cookie的自动管理
                //.addInterceptor(interceptor)
                .build();
        Retrofit retrofit=new Retrofit.Builder()
                .baseUrl("http://www.slgluo.xyz/leave/")
                .addConverterFactory(GsonConverterFactory.create())
                .addCallAdapterFactory(RxJavaCallAdapterFactory.create())
                .client(okHttpClient)
                .build();
        qjiaService=retrofit.create(QjiaService.class);
        return qjiaService;
    }

    @Override
    public boolean isNetOk(Context context) {
        ConnectivityManager cm=(ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE);//获得链接设备管理对象
        NetworkInfo current=cm.getActiveNetworkInfo();
        if(current!=null){
            return true;
        }

        return false;
    }
}
